package com.pl.myshop.business.reg.service.controller;

import com.pl.myshop.commons.dto.ResponseResult;
import com.pl.myshop.provider.api.RegService;
import com.pl.myshop.provider.domain.UmsAdmin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: pl
 * @Date: 2020/3/21 22:49
 * @Description:
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "reg")
public class RegController {

    @Reference(version = "1.0.0")
    private RegService regService;

    @PostMapping(value = "/reginsert")
    public ResponseResult<UmsAdmin> reg(@RequestBody UmsAdmin umsAdmin){
        if (validateReg(umsAdmin)){
            int regInsert = regService.regInsert(umsAdmin);
            if (regInsert>0){
                UmsAdmin admin = regService.get(umsAdmin.getUsername());
                return new ResponseResult<UmsAdmin>(HttpStatus.OK.value(), "新用户注册成功", admin);
            }
        }
        return new ResponseResult<UmsAdmin>(HttpStatus.NOT_ACCEPTABLE.value(), "用户名已存在，请重新输入");
    }

    /**
     * 验证注册信息
     * @param umsAdmin {@link UmsAdmin}
     * @return 错误信息
     */
    private boolean validateReg(UmsAdmin umsAdmin) {
        UmsAdmin admin = regService.get(umsAdmin.getUsername());
        if (admin != null) {
            return false;
        }

        return true;
    }
}